package com.fenghongzhang.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class Main3Activity extends AppCompatActivity {
    private TextView time;
    private Button click;

    private int index = 5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);


        time = (TextView) findViewById(R.id.time);
        click = (Button) findViewById(R.id.click);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        index--;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                time.setText("倒计时:"+index+"秒");
                            }
                        });
                        if (index <= 0){
                            Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
                            startActivity(intent);
                            timer.cancel();
                        }


                    }
                },0,1000);
            }
        });

    }
}
